<?php
namespace app\portal\controller;

use app\portal\model\ExchangeGoodsModel;
use app\portal\model\UserModel;
use app\portal\model\UserRankModel;
use cmf\controller\AdminBaseController;
use think\Db;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/24
 * Time: 16:49
 */
use app\portal\model\PointRuleModel;
use app\portal\model\PointExchangeLogModel;

class AdminPointsController extends AdminBaseController{
    //积分统计
    public function points_list(){
        $model=new UserRankModel();
        $data=$model->with('user')->order('all_points desc')->paginate(15);
        $this->assign('data',$data);
        $this->assign('page',$data->render());
        return $this->fetch();
    }
    //删除积分统计
    public function points_list_del(){
        $Model = new UserRankModel();
        $id= $this->request->param('id');
        $result=$Model->where('id',$id)->delete();
        if ($result) {
            $this->success('删除成功!');
        } else {
            $this->error('删除失败');
        }
    }
    //清空积分统计
    public function set_points_null(){
        $id=$this->request->param('id');
        $res=Db::name('user_rank')->where('id',$id)->update(['duration'=>0,'q_times'=>0,'z_times'=>0,'all_points'=>0,'used_points'=>0,'can_points'=>0]);
        if ($res) {
            $this->success('清空成功!');
        } else {
            $this->error('清空失败');
        }
    }
    //批量删除积分统计
    public function points_list_all_del(){
        $Model = new UserRankModel();
        $ids     = $this->request->param('ids/a');
        $result=$Model->where('id', 'in', $ids)->delete();
        if ($result) {
            $this->success('删除成功!');
        } else {
            $this->error('删除失败');
        }
    }

    //兑换分数记录
    public function index(){
        $where=[];
        $model = new PointExchangeLogModel();
        $user =new UserModel();
        $param = $this->request->param();
        if(!empty($param['keyword'])){
            $user_id=$user->where('user_nickname','like','%'.$param['keyword'].'%')->column('id');
            $where[]=['user_id','in',$user_id];
            $this->assign('keyword',$param['keyword']);
        }
        $data=$model->with('user')->where($where)->order('id desc')->paginate(10);
        $this->assign('page', $data->render());
        $this->assign('data',$data);
        return $this->fetch();

    }
    //兑分规则
    public function rule(){
        $model=new PointRuleModel();
        $data=$model->select();
        //$categories=[['id'=>1,'name'=>'牵手积分兑换规则','description'=>'10小时兑换换1分','condition'=>10],['id'=>'2','name'=>'啄木鸟兑换规则','description'=>'5次兑换一分','condition'=>5]];
        $this->assign('data',$data);
        return $this->fetch();
    }
    //添加/修改规则
    public function rule_add(){
        $Model = new PointRuleModel();
        $id=input('id');
        if($id){
            $data=$Model->find($id);
            $this->assign('data',$data);
            $this->assign('id',$id);
        }
        return $this->fetch();
    }
    //保存规则数据
    public function save_add(){
        $Model = new PointRuleModel();
        $data=input('post.');
//        dump($data);exit;
        if(empty($data['id'])){
            unset($data['id']);
            $result = $Model->insert($data);
            if ($result === false) {
                $this->error('添加失败!');
            }
            $this->success('添加成功!', url('AdminPoints/rule'));
        }else{
            $result = $Model->update($data);
            if ($result === false) {
                $this->error('修改失败!');
            }
            $this->success('修改成功!', url('AdminPoints/rule'));
        }
    }
    //删除兑分规则
    public function rule_del(){
        $Model = new PointRuleModel();
        $id= $this->request->param('id');
        $result=$Model->where('id',$id)->delete();
        if ($result) {
            $this->success('删除成功!');
        } else {
            $this->error('删除失败');
        }
    }
    //商品列表
    public function goods(){
        $model=new ExchangeGoodsModel();
        $status=['<p style="color: orange">下架中</p>','<p style="color: green">上架中</p>'];
        $data=$model->paginate(10);
        $this->assign('status',$status);
        $this->assign('data',$data);
        $this->assign('data',$data);
        return $this->fetch();

    }
    //添加/修改商品
    public function goods_add(){
        $Model = new ExchangeGoodsModel();
        $id=input('id');
        if($id){
            $data=$Model->find($id);
            $this->assign('data',$data);
            $this->assign('id',$id);
        }
        return $this->fetch();
    }
    //保存商品数据
    public function save_goods(){
        $Model = new ExchangeGoodsModel();
        $data=input('post.');
//        dump($data);exit;
        if(empty($data['id'])){
            unset($data['id']);
            $result = $Model->insert($data);
            if ($result === false) {
                $this->error('添加失败!');
            }
            $this->success('添加成功!', url('AdminPoints/goods'));
        }else{
            $result = $Model->update($data);
            if ($result === false) {
                $this->error('修改失败!');
            }
            $this->success('修改成功!', url('AdminPoints/goods'));
        }
    }
    //删除商品
    public function goods_del(){
        $Model = new ExchangeGoodsModel();
        $id= $this->request->param('id');
        $result=$Model->where('id',$id)->delete();
        if ($result) {
            $this->success('删除成功!');
        } else {
            $this->error('删除失败');
        }
    }
    //删除兑分记录
    public function point_exchange_log_del(){
        $Model = new PointExchangeLogModel();
        $id= $this->request->param('id');
        $result=$Model->where('id',$id)->delete();
        if ($result) {
            $this->success('删除成功!');
        } else {
            $this->error('删除失败');
        }
    }
    //批量删除
    public function all_del(){
        $ids     = $this->request->param('ids/a');
        $result  = Db::name('point_exchange_log')->where('id', 'in', $ids)->delete();
        if ($result) {
            $this->success("删除成功！", '');
        }
    }
}